package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppGift;
import com.vf.admin.miaodong.entity.AppPrivilege;
import com.vf.admin.miaodong.entity.GameSetting;
import com.vf.admin.miaodong.entity.GameZpJiangpin;
import com.vf.admin.miaodong.service.AfterGiftService;
import com.vf.admin.miaodong.service.AfterPrivilegeService;
import com.vf.admin.miaodong.service.GameSettingService;
import com.vf.admin.miaodong.service.GameZpJiangpinService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 游戏Controller
 */
@Controller
@RequestMapping(value = "/after/game")

public class AfterGameController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    GameZpJiangpinService gameZpJiangpinService;
    @Autowired
    GameSettingService gameSettingService;
    @Autowired
    AfterPrivilegeService afterPrivilegeService;
    @Autowired
    AfterGiftService afterGiftService;

    /**
     * 获取大转盘奖品列表
     *
     * @param pageBean 分页对象
     * @return
     */
    @RequestMapping("/zpJiangpinList")
    public String list(SplitPageBean pageBean, GameZpJiangpin jiangpin) {

        try {
            Map<String, Object> paraMap = new HashMap<String, Object>(16);
            if (jiangpin != null) {
                paraMap.put("jpname", jiangpin.getJpname());
                paraMap.put("state", jiangpin.getState());
            } else {
                jiangpin = new GameZpJiangpin();
            }

            this.getSplitPageBeanpagesize(pageBean.getPageSize(), pageBean.getPageIndex());
            List<GameZpJiangpin> list = gameZpJiangpinService.findList(paraMap, splitPage);
            Map<Integer, String> shiFouStateMap = Constants.getShiFouStateMap();
            request.setAttribute("jiangpin", jiangpin);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPage);
            request.setAttribute("shiFouStateMap", shiFouStateMap);
            return this.sucess("/web/after/game/zpJiangpinList");
        } catch (Exception e) {
            logger.error("系统异常", e);
            return this.error("系统异常");
        }
    }


    /**
     * 添加大转盘奖品跳转
     *
     * @return
     */
    @RequestMapping("/zpJiangpinAdd")
    public String zpJiangpinAdd() {
        try {
            Map<Integer, String> shiFouStateMap = Constants.getShiFouStateMap();
            List<AppPrivilege> guizulist=afterPrivilegeService.list(new HashMap<>(16),null);
            List<AppGift> liwulist=afterGiftService.findList(new HashMap<>(16),null);
            request.setAttribute("guizulist", guizulist);
            request.setAttribute("liwulist", liwulist);
            request.setAttribute("shiFouStateMap", shiFouStateMap);
            return this.sucess("/web/after/game/zpJiangpinAdd");
        } catch (Exception e) {
            logger.error("系统异常", e);
            return this.error("系统异常");
        }
    }



    /**
     * 添加大转盘奖品跳转
     *
     * @return
     */
    @RequestMapping("/zpJiangpinDetail")
    public String zpJiangpinDetail(Integer id) {
        try {
            Map<Integer, String> shiFouStateMap = Constants.getShiFouStateMap();
            GameZpJiangpin jiangpin=gameZpJiangpinService.get(id);
            List<AppPrivilege> guizulist=afterPrivilegeService.list(new HashMap<>(16),null);
            List<AppGift> liwulist=afterGiftService.findList(new HashMap<>(16),null);
            request.setAttribute("guizulist", guizulist);
            request.setAttribute("liwulist", liwulist);
            request.setAttribute("shiFouStateMap", shiFouStateMap);
            request.setAttribute("jiangpin", jiangpin);
            return this.sucess("/web/after/game/zpJiangpinEdit");
        } catch (Exception e) {
            logger.error("系统异常", e);
            return this.error("系统异常");
        }
    }


    /**
     * @return com.vf.admin.common.entity.Result
     * @撰写人 你猜
     * @Description //TODO 大转盘奖品保存
     * @时间 9:53 2021/3/10
     * @Param [userId, vipId]
     **/
    @RequestMapping("/zpJiangpinAddSave")
    @ResponseBody
    public Result zpJiangpinAddSave(String jiangpinName, String values, Integer state, Integer type, HttpServletRequest request) {
        try {


            GameZpJiangpin jiangpin=new GameZpJiangpin();
            jiangpin.setState(state);
            jiangpin.setJpname(jiangpinName);
            jiangpin.setJpzhi(values);
            jiangpin.setLeixing(type);
            switch (type){
                case 1:
                    //钻石
                    jiangpin.setJpimg("http://vifei-updoor.oss-cn-beijing.aliyuncs.com/16042830323039v3Cn2z4Y89f1.jpg");
                    break;
                case 2:
                    //金币
                    jiangpin.setJpimg("http://vifei-updoor.oss-cn-beijing.aliyuncs.com/1604283126791R5vY2LDE39v61.jpg");
                    break;
                case 3:
                    //礼物
                    AppGift gift=afterGiftService.get(Integer.valueOf(values));
                    jiangpin.setJpimg(gift.getPicture());
                    break;
                case 4:
                    //贵族
                    AppPrivilege privilege=afterPrivilegeService.get(Integer.valueOf(values));
                    jiangpin.setJpimg(privilege.getMedal());
                    break;
                default:
            }
            jiangpin.setCanyustate(0);
            gameZpJiangpinService.save(jiangpin);
            return sucessJSON("成功");
        } catch (Exception e) {
            logger.error("系统异常", e);
            return this.errorJSON("系统异常");
        }
    }



    /**
     * @return com.vf.admin.common.entity.Result
     * @撰写人 你猜
     * @Description //TODO 大转盘奖品修改保存
     * @时间 9:53 2021/3/10
     * @Param [userId, vipId]
     **/
    @RequestMapping("/zpJiangpinUpdateSave")
    @ResponseBody
    public Result zpJiangpinUpdateSave(Integer id, String jiangpinName, String values, Integer state, Integer type, HttpServletRequest request) {
        try {

            GameZpJiangpin jiangpin=new GameZpJiangpin();
            jiangpin.setId(id);
            jiangpin.setState(state);
            jiangpin.setJpname(jiangpinName);
            jiangpin.setJpzhi(values);
            jiangpin.setLeixing(type);
            jiangpin.setCanyustate(0);
            switch (type){
                case 1:
                    //钻石
                    jiangpin.setJpimg("http://vifei-updoor.oss-cn-beijing.aliyuncs.com/16042830323039v3Cn2z4Y89f1.jpg");
                    break;
                case 2:
                    //金币
                    jiangpin.setJpimg("http://vifei-updoor.oss-cn-beijing.aliyuncs.com/1604283126791R5vY2LDE39v61.jpg");
                    break;
                case 3:
                    //礼物
                    AppGift gift=afterGiftService.get(Integer.valueOf(values));
                    jiangpin.setJpimg(gift.getPicture());
                    break;
                case 4:
                    //贵族
                    AppPrivilege privilege=afterPrivilegeService.get(Integer.valueOf(values));
                    jiangpin.setJpimg(privilege.getMedal());
                    break;
                default:
            }

            gameZpJiangpinService.save(jiangpin);
            return sucessJSON("成功");
        } catch (Exception e) {
            logger.error("系统异常", e);
            return this.errorJSON("系统异常");
        }
    }


    /**
     * 修改转盘游戏参数-跳转
     *
     * @return
     */
    @RequestMapping("/updateZpCanShu")
    public String updateZpCanShu() {
        try {
            GameSetting gameSetting =gameSettingService.get();
            System.out.println(gameSetting.getZuanshinum());
            request.setAttribute("gameSetting", gameSetting);
            return this.sucess("/web/after/game/zpEdit");
        } catch (Exception e) {
            logger.error("系统异常", e);
            return this.error("系统异常");
        }
    }

    /**
     * @return com.vf.admin.common.entity.Result
     * @撰写人 你猜
     * @Description //TODO 修改转盘游戏参数修改
     * @时间 9:53 2021/3/10
     * @Param [userId, vipId]
     **/
    @RequestMapping("/updateZpCanShuSave")
    @ResponseBody
    public Result updateZpCanShuSave(String zuanshinum, HttpServletRequest request) {
        try {

            gameSettingService.xiugai(zuanshinum);
            return errorJSON("成功");
        } catch (Exception e) {
            logger.error("系统异常", e);
            return this.errorJSON("系统异常");
        }
    }

//概率设置===========

    /**
     * 大转盘概率设置跳转
     *
     * @return
     */
    @RequestMapping("/zpGailvSetting")
    public String zpGailvSetting() {
        try {
            Map<String, Object> paraMap = new HashMap<String, Object>(16);
            paraMap.put("state", 1);
            List<GameZpJiangpin> list = gameZpJiangpinService.findList(paraMap, splitPage);
            request.setAttribute("list", list);
            return this.sucess("/web/after/game/zpGailvSetting");
        } catch (Exception e) {
            logger.error("系统异常", e);
            return this.error("系统异常");
        }
    }

    /**
     * @return com.vf.admin.common.entity.Result
     * @撰写人 你猜
     * @Description //TODO 大转盘概率保存
     * @时间 9:53 2021/3/10
     * @Param [userId, vipId]
     **/
    @RequestMapping("/zpGailvSettingSave")
    @ResponseBody
    public Result zpGailvSettingSave(String chushiGlValue, HttpServletRequest request) {
        try {
            System.out.println("&："+chushiGlValue);
            String[] shuzu = chushiGlValue.split(",");
            for (int i = 0, len = shuzu.length; i < len; i++) {
                String a=shuzu[i].substring(0,shuzu[i].indexOf("α"));
                String b=shuzu[i].substring(shuzu[i].indexOf("α")+1,shuzu[i].indexOf("β"));
                String c=shuzu[i].substring(shuzu[i].indexOf("β")+1,shuzu[i].indexOf("γ"));
                String d=shuzu[i].substring(shuzu[i].indexOf("γ")+1,shuzu[i].length());
                gameZpJiangpinService.modificationProbability(Integer.valueOf(a),b,c,d);
            }
            return sucessJSON("成功");
        } catch (Exception e) {
            logger.error("系统异常", e);
            return this.errorJSON("系统异常");
        }
    }


}
